package com.example.hao.retrofitplus;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;


import com.example.hao.retrofitplus.pojo.ResultData;
import com.example.hao.retrofitplus.utils.RetrofitClient;

import java.io.File;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "Hao";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        test1();
//        test2();
//        test3();
    }

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://192.168.3.100:8080/home/")
            //
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            //指定转换流
            .addConverterFactory(GsonConverterFactory.create()).build();

    /**
     * Retrofit 实现Get 请求
     */
    private void test1() {

        RetrofitClient.getClient().register("小明", "小华", new Consumer<ResultData>() {
            @Override
            public void accept(ResultData resultData) throws Exception {

                String s = resultData.toString();
                Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
                Log.d(TAG, "accept: "+ s);
            }
        });



//        APIRequestService apiRequestService = retrofit.create(APIRequestService.class);//动态代理
//
//        Disposable disposable = apiRequestService.register("XiaoMing", "666")
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Consumer<ResultData>() {
//                    @Override
//                    public void accept(ResultData resultData) throws Exception {
//
//                        Log.d(TAG, "accept: " + resultData.toString());
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        Log.e(TAG, "accept: " + throwable.getMessage());
//                    }
//                });


    }

    private void test2() {

        RetrofitClient.getClient().getUser("小明", new Consumer<ResultData>() {
            @Override
            public void accept(ResultData resultData) throws Exception {
                String s = resultData.toString();
                Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
                Log.d(TAG, "accept: "+s);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                String message = throwable.getMessage();
                Log.e(TAG, "accept: " +message);
            }
        });
//        APIRequestService apiRequestService = retrofit.create(APIRequestService.class);
//
//        Observable<ResultData<User>> observable = apiRequestService.getUser("hahaha");
//
//        Disposable disposable = observable
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Consumer<ResultData<User>>() {
//                    @Override
//                    public void accept(ResultData<User> userResultData) throws Exception {
//                        Log.d(TAG, "accept: " + userResultData.toString());
//                    }
//                });


    }

    public static final MediaType TYPE_FILE = MediaType.parse("application/octet-stream");

    private void test3() {
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "android1.png");
        RetrofitClient.getClient().uploadIcon("晓明", file, new Consumer<ResultData>() {
            @Override
            public void accept(ResultData resultData) throws Exception {
                String s = resultData.toString();
                Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
                Log.d(TAG, "accept: "+ s);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                Log.e(TAG, "accept: "+throwable.getMessage() );
            }
        });
//        APIRequestService apiRequestService = retrofit.create(APIRequestService.class);
//
//        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "android1.png");
//
//        final RequestBody requestBody = RequestBody.create(TYPE_FILE, file);
//
//
//        MultipartBody.Part formData = MultipartBody.Part.createFormData("android1", file.getName(), requestBody);
//
//
//        Disposable disposable = apiRequestService.uploadIcon("机器人", formData)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Consumer<ResultData>() {
//                    @Override
//                    public void accept(ResultData resultData) throws Exception {
//                        Log.d(TAG, "onResponse: " + resultData.toString());
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        Log.e(TAG, "accept: " + throwable.getMessage());
//                    }
//                });


    }
}
